﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace BakalarskaPraca.Generation.Helpers
{
    public static class ExtraTagDataRetrieval
    {
        private static readonly string TagIdentifier = Properties.Settings.Default.ExtraTagIdentifier;
    
        public static bool CheckForExtraTag(XmlElement element)
        {
            XmlNode[] extraTags = element.ChildNodes.Cast<XmlNode>().Where(e => e.Name.Equals("extra")).ToArray();
            if (extraTags.Any())
            {
                return
                    extraTags.Any(
                        extraTag => extraTag.ChildNodes.Cast<XmlNode>().Any(            
                            elem =>
                            elem.Name.Equals("technique") &&
                        ((XmlElement) elem).GetAttribute("profile").Equals(Properties.Settings.Default.ExtraTagIdentifier)));
            }
            return false;
        }

        public static List<KeyValuePair<string, string>> GetGenerationInstructions(XmlElement element)
        {
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            XmlNode[] extraTags = element.ChildNodes.Cast<XmlNode>().Where(e => e.Name.Equals("extra")).ToArray();
            extraTags.ToList().ForEach(
                    extraTag => extraTag.ChildNodes.Cast<XmlNode>().ToList().ForEach(
                        elem =>
                            {
                                if (elem.Name.Equals("technique") &&
                                    ((XmlElement) elem).GetAttribute("profile").Equals(TagIdentifier))
                                    elem.ChildNodes.Cast<XmlNode>().ToList().ForEach(
                                        instruction =>
                                        result.Add(new KeyValuePair<string, string>(instruction.Name, instruction.InnerText)));
                            }));
            return result;
        }


    }
}
